
var cancalDistance = 120;
function fnLongPressRecording() {
    //中间黑色边框和里面的内容（录音状态）
    var blackBoxSpeak = document.querySelector(".blackBoxSpeak");
    blackBoxSpeak.style.background = "url('../../image/im/ic_record.png') no-repeat 28 16px/65px 104px, url('../../image/im/ic_record_ripple_9.png')no-repeat 111.2px 32px/28.8px 88px";
    blackBoxSpeak.style.backgroundColor = "rgba(0,0,0,.7)";

    //中间黑色边框和里面的内容（暂停状态）
    var blackBoxPause = document.querySelector(".blackBoxPause");
    blackBoxPause.style.background = "rgba(0,0,0,.7) url('../../image/im/ic_release_to_cancel.png')no-repeat center 8px/67.2px 104px";
    blackBoxPause.style.backgroundColor = "rgba(0,0,0,.7)";

    //手指移动相关
    var posStart = 0; //初始化起点坐标
    var posEnd = 0; //初始化终点坐标
    var posMove = 0; //初始化滑动坐标

    //轮播相关
    var index = [9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var num = index.length;
    var timer = null; //用于清除计时器
    var recorder = api.require('recMp3');
    var animation = {};
    function initEvent() {
        animation.touchstart = function() {
            var resultList = api.hasPermission({
                list: ['microphone']
            });
            if(resultList[0]['granted']+'' != 'false') {
                setTimer();
                posStart = 0;
                recorder.start(function(ret, err) {
                    if(!ret) {
                        console.error('打开录音失败>>>>'+ err.message);
                        api.requestPermission({
                            list: ['microphone']
                        },function(ret) {
                            if(ret && ret.list) {
                                var cancle = []
                                for(var i = 0; i < ret.list.length; i ++) {
                                    if(!ret.list[i]['granted']) {
                                        cancle.push(ret.list[i]['name']);
                                    }
                                }
                                if(cancle.length >0) {
                                    fnToast('您没有允许录音权限，请允许后继续');
                                }
                            }
                        })
                    }
                });
                //显示录音 隐藏暂停
                showBlackBoxSpeak();
            }else {
                api.requestPermission({
                    list: ['microphone']
                },function(ret) {
                    if(ret && ret.list) {
                        var cancle = []
                        for(var i = 0; i < ret.list.length; i ++) {
                            if(!ret.list[i]['granted']) {
                                cancle.push(ret.list[i]['name']);
                            }
                        }
                        if(cancle.length >0) {
                            fnToast('您没有允许录音权限，请允许后继续');
                        }
                    }
                })
            }
        };

        animation.touchmove = function(isOut) {
            if(!isOut) {
                //隐藏录音 显示暂停
                showBlackBoxSpeak();
            } else {
                //显示录音 隐藏暂停
                showBlackBoxPause();
            }
        };

        animation.touchend = function(isCancel) {
            clearInterval(timer);
            //初始化状态
            initStatus();

            if(!isCancel) {
                console.log("发送录音");
                recorder.stop(function(ret) {
                    // 时长
                    var duration = ret.duration;
                    if(duration == 0) {
                        fnToast('录音时间太短');
                    }else {
                        // 音频路径
                        var path = ret.path;
                        api.sendEvent({
                            name: 'imRecorderSend',
                            extra: {
                                duration: duration,
                                path: path
                            }
                        })
                    }
                });
                showBlackBoxNone();
            } else {
                console.log("录音取消发送");
                recorder.stop();
                showBlackBoxNone();
            }
        };
    }
    initEvent();
    window.animation = animation;

    //轮播
    function setTimer() {
        timer = setInterval(function() {
            setTimeout(function() {
                num++;
                blackBoxSpeak.style.background = "url('../../image/im/ic_record.png')no-repeat 28px 16px/64px 104px, url('../../image/im/ic_record_ripple_" + index[num] + ".png')no-repeat 111.2px 32px/28.8px 88px";
                blackBoxSpeak.style.backgroundColor = " rgba(0,0,0,.7)";
            }, 70);
            if(num >= index.length - 1) {
                num = 0;
            }
        }, 200);
    }

    //初始化状态
    var initStatus = function() {        
        //全部隐藏
        showBlackBoxNone();
    }

    //显示录音 隐藏暂停
    var showBlackBoxSpeak = function() {
        blackBoxSpeak.style.display = "block";
        blackBoxPause.style.display = "none";
    }

    //隐藏录音 显示暂停
    var showBlackBoxPause = function() {
        blackBoxSpeak.style.display = "none";
        blackBoxPause.style.display = "block";
    }

    //隐藏录音
    var showBlackBoxNone = function() {
        blackBoxSpeak.style.display = "none";
        blackBoxPause.style.display = "none";
    }
}